package org.mineground.world;

import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.mineground.Main;
import org.mineground.Utilities;

/**
 * @file Warp.java (18.02.2012)
 * @author Daniel Koenen
 *
 */
public class Warp {
    private int warpId;
    private int X;
    private int Y;
    private int Z;

    private float yaw;
    private float pitch;

    private long password;

    private String creator;
    private String name;

    private boolean isPublic;

    private World world;

    public Warp(int warpId, int X, int Y, int Z, float yaw, float pitch, String world, String creator, String name, long password, boolean isPublic) throws Exception {
	this.warpId = warpId;

	this.X = X;
	this.Y = Y;
	this.Z = Z;

	this.yaw = yaw;
	this.pitch = pitch;

	this.creator = creator;
	this.name = name;
	this.password = password;

	this.isPublic = isPublic;

	this.world = Main.getInstance().getServer().getWorld(world);
        
	if (this.world == null) {
	    throw new Exception("Invalid world given.");
	}

	if (this.world.getBlockAt(X, Y, Z) == null) {
	    throw new Exception("Invalid location given.");
	}
    }

    public void setWorld(String world) {
	this.world = Main.getInstance().getServer().getWorld(world);
    }

    public void setPassword(String pw) {
	setPassword(pw.hashCode());
    }

    public void setName(String name) {
	this.name = name;
    }

    public void setPublic(boolean toggle) {
	isPublic = toggle;
    }

    public int getX() {
	return X;
    }

    public int getY() {
	return Y;
    }

    public int getZ() {
	return Z;
    }

    public int getId() {
	return getWarpId();
    }

    public float getYaw() {
	return yaw;
    }

    public float getPitch() {
	return pitch;
    }

    public long getPassword() {
	return password;
    }

    public String getName() {
	return name;
    }

    public String getCreator() {
	return creator;
    }

    public boolean getPublic() {
	return isPublic;
    }

    public String getWorld() {
	return world.getName();
    }

    public void teleport(Player player) {
	if (world.getBlockAt(X, Y, Z).getType() != Material.AIR) {
	    player.sendMessage(ChatColor.RED + "* Error: This warp had an invalid spawn location, it has been fixed.");
	    world.getBlockAt(X, Y, Z).setType(Material.AIR);
	}

	player.teleport(new Location(world, X, Y, Z, yaw, pitch));
        
        if (name.equalsIgnoreCase("home_" + player.getName().hashCode())) {
            StringBuilder inform = new StringBuilder();
            inform.append(ChatColor.LIGHT_PURPLE);
            inform.append("* Welcome home (");
            inform.append(getWarpId());
            inform.append(").");
            player.sendMessage(inform.toString());

            inform = new StringBuilder();
            inform.append(Colors.BROWN);
            inform.append("* ");
            inform.append(Utilities.fixName(player));
            inform.append(Colors.BROWN);
            inform.append(" has been warped to his/her home warp (");
            inform.append(getWarpId());
            inform.append(").");
            Main.getInstance().getIRCHandler().sendMessage("@" + Main.getInstance().getConfigHandler().ircChannel, inform.toString());
            return;
        }
        
	StringBuilder inform = new StringBuilder();
	inform.append(ChatColor.LIGHT_PURPLE);
	inform.append("* Welcome to '");
	inform.append(name);
	inform.append("' (");
	inform.append(getWarpId());
	inform.append(").");
	player.sendMessage(inform.toString());

	inform = new StringBuilder();
	inform.append(Colors.BROWN);
	inform.append("* ");
	inform.append(Utilities.fixName(player));
	inform.append(Colors.BROWN);
	inform.append(" has been warped to '");
	inform.append(name);
	inform.append("' (");
	inform.append(getWarpId());
	inform.append(").");
	Main.getInstance().getIRCHandler().sendMessage("@" + Main.getInstance().getConfigHandler().ircChannel, inform.toString());
    }

    public boolean isWarpAllowed(Player player) {
	if (!isPublic && !player.getName().equals(creator)) {
	    return false;
	}

	return true;
    }

    public boolean hasPassword() {
	return (password == 0) ? (false) : (true);
    }

    public boolean isCorrectPassword(String pw) {
	if (pw.hashCode() == password) {
	    return true;
	}

	return false;
    }

    public int getWarpId() {
        return warpId;
    }

    public void setX(int X) {
        this.X = X;
    }

    public void setY(int Y) {
        this.Y = Y;
    }

    public void setZ(int Z) {
        this.Z = Z;
    }

    public void setYaw(float yaw) {
        this.yaw = yaw;
    }

    public void setPitch(float pitch) {
        this.pitch = pitch;
    }

    public void setPassword(long password) {
        this.password = password;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }
}
